package com.personal.website.lottery.table

import java.util
import scala.collection.JavaConverters._

/**
 * Created by Gavin on 2015/12/14.
 */
class BlueTable(issue: Int,redBalls: Seq[Int], blueBall: Int) extends Table(issue,redBalls,blueBall){
  override def createRecord() = createOneToSixteen ++ createEightDigitAnalysis ++ createEight012 ++ createFrontBackSection ++
    createFourDigitAnalysis ++  createOdd ++ createEven ++ create012 ++ createSectionAnalysis

  def createOneToSixteen = BlueTable.ALL_EXISTS_BALLS.map(ball => if(ball == blueBall) ball else DEFAULT_NON_EXISTS_VALUE)
  def createEightDigitAnalysis = (1 to 8).map(digit => if(blueBall % 8 + 1 == digit) digit else DEFAULT_NON_EXISTS_VALUE)
  def createEight012 = (0 to 2).map(digit => if(createEightDigitAnalysis.filter(_ != DEFAULT_NON_EXISTS_VALUE).head % 3 == digit) digit else DEFAULT_NON_EXISTS_VALUE)
  def createFrontBackSection = (0 to 1).map(digit => if(createEightDigitAnalysis.count(x => x != DEFAULT_NON_EXISTS_VALUE && x > digit * 4 && x <= (digit + 1) * 4) > 0) 1 else DEFAULT_NON_EXISTS_VALUE)
  def createFourDigitAnalysis = (1 to 4).map(digit => if(blueBall % 4 + 1 == digit) digit else DEFAULT_NON_EXISTS_VALUE)
  def createOdd = Seq(if(createFourDigitAnalysis.filter(_ != DEFAULT_NON_EXISTS_VALUE).head % 2 == 1) 1 else DEFAULT_NON_EXISTS_VALUE)
  def createEven = Seq(if(createOdd.head == DEFAULT_NON_EXISTS_VALUE) 1 else DEFAULT_NON_EXISTS_VALUE)
  def create012 = (0 to 2).map(digit => if(blueBall % 3 == digit) digit else DEFAULT_NON_EXISTS_VALUE)
  def createSectionAnalysis = (1 to 4).map(digit => if(blueBall <= digit * 4 && blueBall > (digit -1) * 4) digit else DEFAULT_NON_EXISTS_VALUE)

  override def toString: String = Seq(Seq(issue),createRecord()).flatten.mkString(";")
}

object BlueTable{
  def apply(issue: Int,redBalls: Seq[Int], blueBall: Int) = new BlueTable(issue,redBalls,blueBall)
  def apply(issue: Int,allBalls: Seq[Int]) = new BlueTable(issue,allBalls.init,allBalls.last)
  def newInstance(issue: Integer,redBalls: util.List[Integer],blueBall: Integer) = new BlueTable(issue.toInt, redBalls.asScala.map(_.toInt),blueBall.toInt)

  val ALL_EXISTS_BALLS = (1 to 16)
  val headString = Seq("issue",(1 to 16).mkString("|"),"eightDigitAnalysis","eight012","frontBackSection","fourDigitAnalysis","odd","even","012","sectionAnalysis")
}